#version 450 compatibility




layout(local_size_x = DEF_LocalSize, local_size_y = 8, local_size_z = 1) in;


layout( binding = 0, rgba8ui) uniform uimage2D Pix;

layout( binding = 1 ) uniform Buf_LoalParameter1 {
    ivec2   m_Offset;
    ivec2   m_WidgetOffset;
    uvec2   m_Color;
    uint    m_DataNum;
    uint    m_Channel;

    float   m_SamplingGap;
    float   m_Gain;
    float   m_LineMaxWidth;
};

layout( binding = 2 ) buffer Buf_LoalParameter2 {
    float   m_Intensity[];
};



//shared uint id = 0;
shared bool Break = false;
shared bool Even = false;
shared float dataID = 0;
shared float Sound[8];
//shared float Scale = 1.0;


void main(){
    //const uint localID = gl_LocalInvocationID.y*gl_WorkGroupSize.x + gl_LocalInvocationID.x;
    const uint localID = gl_LocalInvocationID.x;
    
    if(localID == 0){
        uint id = gl_WorkGroupID.x;
        if ((id&1) > 0) {
            Even = true;
        }
        
        if(m_WidgetOffset.x < 0) {
            id += abs(m_WidgetOffset.x);
        }
        //id = uint(float(id) * m_SamplingGap);
        dataID = id;

        

        if(id<m_DataNum){
            
            
            if (gl_LocalInvocationID.y == 0) {
                if(m_Channel == 0){
                    Sound[0] = m_Intensity[uint(dataID)];
                    Sound[0] *= float(gl_NumWorkGroups.y);
                    Sound[0] *= m_Gain;

                } else if(m_Channel == 1){
                    Sound[0] = m_Intensity[uint(dataID)];
                    //if(abs(Sound[0]) < 40 ) Sound[0] = 0.0;
                    //Sound[0] = Sound[0]/32767.0;
                    Sound[0] *= float(gl_NumWorkGroups.y);
                    Sound[0] *= m_Gain;
                    Sound[0] += gl_NumWorkGroups.y/2;
                }
                

            } else if(m_Channel == 2 && gl_LocalInvocationID.y < m_Channel) {

                Sound[gl_LocalInvocationID.y] = m_Intensity[uint(dataID)];
                Sound[gl_LocalInvocationID.y] += 32767.0*m_Gain;
                Sound[gl_LocalInvocationID.y] /= 65535.0*m_Gain;
                Sound[gl_LocalInvocationID.y] *= float(gl_NumWorkGroups.y/m_Channel);

            }
        } else {
            Break = true;
        }
        
    }
    
    barrier();
    if(Break) return;


    //---------gl_LocalInvocationID.y---------声道---------------

    if(gl_LocalInvocationID.y < m_Channel || m_Channel == 0){
        //每个声道所占用组件高度
        int HeightOffset = 0;
        if(m_Channel > 0){
            HeightOffset = int((gl_NumWorkGroups.y/m_Channel) * gl_LocalInvocationID.y);
        }
        
        
        //声压强度+声压所占组件高度
        int height = int(Sound[gl_LocalInvocationID.y]) + HeightOffset;
        if(gl_WorkGroupID.y < HeightOffset || gl_WorkGroupID.y >= height) return;

        //
        ivec2 coord = ivec2(gl_WorkGroupID.x, gl_WorkGroupID.y);
        
        
        if ( m_SamplingGap < 1 ) {
            coord.x = int(float(coord.x) / abs(m_SamplingGap));
        }

        barrier();
        
        uvec4 c = uvec4(bitfieldExtract(m_Color.x, 0, 8), 
                        bitfieldExtract(m_Color.x, 8, 8), 
                        bitfieldExtract(m_Color.x, 16, 8), 
                        bitfieldExtract(m_Color.x, 24, 8));
        
        uvec4 cA = uvec4(bitfieldExtract(m_Color.y, 0, 8), 
                        bitfieldExtract(m_Color.y, 8, 8), 
                        bitfieldExtract(m_Color.y, 16, 8), 
                        bitfieldExtract(m_Color.y, 24, 8));
        float i_alp = 0.0;
        if(m_Channel > 0){
            i_alp = ( (float(coord.y-HeightOffset)/(float(gl_NumWorkGroups.y/m_Channel))) );
        }else{
            i_alp = ( (float(coord.y-HeightOffset)/(float(gl_NumWorkGroups.y))) );
        }
        
        
        float alp = (1.0 - i_alp)*2;
        i_alp *= 2;
        c.r = uint(float(cA.r)*(alp) + float(c.r)*i_alp);
        c.g = uint(float(cA.g)*(alp) + float(c.g)*i_alp);
        c.b = uint(float(cA.b)*(alp) + float(c.b)*i_alp);

        
        alp = 0.5;
        i_alp = 1.0 - alp;

        
        if (Even) {
            alp /= 2.0;
        }
        coord.x += int(localID);
        
        
        uvec4 color = imageLoad(Pix, coord);
        c.r = uint(float(color.r)*i_alp + float(c.r)*alp);
        c.g = uint(float(color.g)*i_alp + float(c.g)*alp);
        c.b = uint(float(color.b)*i_alp + float(c.b)*alp);
        
        imageStore(Pix, coord, c);
    
    
    }

}





